/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.service.content.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.constant.ConfigCode;
import org.grayrabbit.cms.dao.content.ContentAttachDao;
import org.grayrabbit.cms.entity.Config;
import org.grayrabbit.cms.entity.Content;
import org.grayrabbit.cms.entity.ContentAttach;
import org.grayrabbit.cms.entity.OperLog;
import org.grayrabbit.cms.service.content.ContentAttachService;
import org.grayrabbit.cms.service.system.impl.ConfigContext;
import org.grayrabbit.cms.vo.content.ContentAttachBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.service.impl.GenericServiceImpl;

/**
 * <b>OperLog</b>的service实现类，事务加此处：
 * 
 * @version 1.0,创建时间: 2011-05-16 14:40:08
 */
@Service
@Transactional
public class ContentAttachServiceImpl extends GenericServiceImpl implements ContentAttachService {
	/**
	 * 注入dao层：
	 */
	@Autowired
	@Qualifier("contentAttachDaoImpl")
	private ContentAttachDao contentAttachDao;

	/**
	 * 此方法执行时为只读事务：
	 * 
	 * @see ContentAttachService#getOperLog(String)
	 */
	@Transactional(readOnly = true)
	public ContentAttach getContentAttach(final String attachId) {
		ContentAttach contentAttach = contentAttachDao.findById(attachId);
		contentAttachDao.initialize(contentAttach);
		return contentAttach;
	}

	/**
	 * @see ContentAttachService#delete(String)
	 */
	@Transactional
	public void delete(final ContentAttach contentAttach) {
		String url = contentAttach.getAttachUrl();
		String obj = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
		List<ContentAttach> list = contentAttachDao.findAutoComplateAttach(obj);
		for (ContentAttach ca : list) {
			contentAttachDao.makeTransient(ca);
		}
		contentAttachDao.makeTransient(contentAttach);
	}

	/**
	 * @see ContentAttachService#save(OperLog)
	 */
	@Transactional
	public void save(final ContentAttach contentAttach) {
		contentAttachDao.makePersistent(contentAttach);
	}

	/**
	 * 此方法执行时为只读事务：
	 * 
	 * @see ContentAttachService#getPage(ContentAttachBean)
	 */
	@Transactional(readOnly = true)
	public Page<ContentAttach> getPage(final ContentAttachBean contentAttachBean) {
		Page<ContentAttach> page = new Page<ContentAttach>();
		page.setTotal(contentAttachDao.count(contentAttachBean));
		page.setResult(contentAttachDao.findByQc(contentAttachDao.assembly(contentAttachBean)));
		return page;
	}

	/**
	 * 此方法执行时为只读事务：
	 * 
	 * @see ContentAttachService#list(Page, ContentAttachBean)
	 */
	public void list(Page<ContentAttach> page, ContentAttachBean contentAttachBean) {
		if (page == null)
			return;
		if (page.isAutoCount()) {
			page.setTotal(contentAttachDao.count(contentAttachBean));
		}
		page.setResult(contentAttachDao.findByPage(page, contentAttachBean));
	}

	/**
	 * 判断附件是否存在
	 * 
	 * @param contentAttachBean
	 */
	public boolean validate(ContentAttachBean contentAttachBean) {
		List<ContentAttach> items = contentAttachDao.findContentAttch(contentAttachBean);
		if (items != null && items.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 根据主键id列删除，批量删除OperLog
	 * 
	 * @see ContentAttachService#delete(String[])
	 */
	public void delete(final String[] key) {
		int i = 0;
		for (String id : key) {
			contentAttachDao.makeTransient(contentAttachDao.findById(id));
			i++;
			if (i == MAX_CLEAR)
				contentAttachDao.clear();
		}

	}

	/**
	 * 更新附件信息，填写文章ID、附件描述
	 * 
	 * @param contentAttachId
	 * @param contentAttachMemo
	 */
	public void updateContentAttach(String contentAttachId, String contentAttachMemo, Content content) {
		if (StringUtils.isNotBlank(contentAttachId)) {
			String[] cId = contentAttachId.split("@");
			if (StringUtils.isNotBlank(contentAttachMemo)) {
				String[] cMemo = contentAttachMemo.split("@");
				if (cId.length != cMemo.length) {
					for (int i = 0; i < cId.length; i++) {
						if (StringUtils.isNotBlank(cId[i])) {
							ContentAttach item = contentAttachDao.findById(cId[i]);
							item.setContent(content);
							item.setAttachDownloadNum(0);
							contentAttachDao.makePersistent(item);
						}
					}
				} else {
					for (int i = 0; i < cId.length; i++) {
						if (StringUtils.isNotBlank(cId[i])) {
							ContentAttach item = contentAttachDao.findById(cId[i]);
							if (StringUtils.isNotBlank(cMemo[i])) {
								item.setAttachMemo(cMemo[i]);
							}
							item.setContent(content);
							item.setAttachDownloadNum(0);
							contentAttachDao.makePersistent(item);
						}
					}
				}
			} else {
				for (int i = 0; i < cId.length; i++) {
					if (StringUtils.isNotBlank(cId[i])) {
						ContentAttach item = contentAttachDao.findById(cId[i]);
						item.setContent(content);
						item.setAttachDownloadNum(0);
						contentAttachDao.makePersistent(item);
					}
				}
			}
		}

	}

	/**
	 * 根据文章Id查询上传文件路径
	 * 
	 * @return
	 */
	public List<ContentAttach> ftlFindContentImg(String contentId, int num) {
		return contentAttachDao.ftlFindContentImg(contentId, num);
	}

	public void setPictureToFocus(String contentId, String attachId) {
		if (StringUtils.isBlank(contentId) || StringUtils.isBlank(attachId))
			return;
		ContentAttach attach = contentAttachDao.findById(attachId);
		String url = attach.getAttachUrl();
		url = url.substring(0, url.lastIndexOf("."));
		List<ContentAttach> list = contentAttachDao.findContentAttaches(contentId);
		for (ContentAttach ca : list) {
			if (StringUtils.isBlank(ca.getAttachUrl()))
				continue;
			if (ca.getAttachUrl().startsWith(url)) {
				ca.setFocusPic(new Boolean(true));
			} else if (ca.getFocusPic() == null || ca.getFocusPic().booleanValue()) {
				ca.setFocusPic(new Boolean(false));
			} else {
				continue;
			}
			this.save(ca);
		}
	}

	public List<ContentAttach> ftlLoadContentAttaches(String contentId, String type) {
		if (StringUtils.isBlank(contentId))
			return new ArrayList<ContentAttach>();
		return contentAttachDao.loadContentAttaches(contentId, type);
	}

	public List<ContentAttach> ftlLoadAttachesExpPic(String contentId) {
		if (StringUtils.isBlank(contentId))
			return new ArrayList<ContentAttach>();
		Config suffix = ConfigContext.get(ConfigCode.PIC_TYPE);
		String fix = null;
		if (suffix != null)
			fix = suffix.getConfigAttrValue();
		if (StringUtils.isBlank(fix))
			fix = "jpg|png|gif";
		String[] sufs = fix.split("\\|");
		return contentAttachDao.loadAttachesExpPic(contentId, sufs);
	}
	
	public List<ContentAttach> ftlLoadAttachesExpPic2(String contentId) {
		return contentAttachDao.loadAttachesExpPic2(contentId);
	}
}